#region License

/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#endregion

using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace Mojo.MovieFinder
{
    public class ColonDelimitedMovieFinder : IMovieFinder
    {
        private FileInfo movieFile;
        private List<Movie> movies;

        public ColonDelimitedMovieFinder (FileInfo file)
        {
            MovieFile = file;
        }

        public FileInfo MovieFile 
        {
            get 
            {
                return movieFile;
            }
            set 
            {
                movieFile = value;
                if (movieFile != null && movieFile.Exists) 
                {
                    InitList ();
                }
            }
        }

        public IEnumerable<Movie> FindAll ()
        {
            return movies;
        }

        private void InitList ()
        {
            movies = new List<Movie>();
            using (StreamReader reader = MovieFile.OpenText ()) 
            {
                string line = null;
                while ((line = reader.ReadLine ()) != null) 
                {
                    string[] tuple = line.Split(':');
                    Movie movie = new Movie (tuple [0], tuple [1]);
                    movies.Add (movie);
                }
            }
        }
    }
}
